package cn.hxzy.teacher.examination.service.impl;

import cn.hxzy.teacher.examination.mapper.examinationMapper;
import cn.hxzy.teacher.examination.pojo.Examination;
import cn.hxzy.teacher.examination.pojo.ExaminationDetail;
import cn.hxzy.teacher.examination.service.examinationService;
import org.apache.poi.hpsf.Decimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class examinationServiceimpl implements examinationService {


    @Autowired
    private examinationMapper examMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    //条件查询题库
    @Override
    public Map selectsubjects01(Map map) {
        Integer pageno = (Integer) map.get("pageno");
        Integer pagesize = (Integer) map.get("pagesize");

        Integer aa = (pageno - 1) * pagesize;
        map.put("pageno", aa);
        map.put("pagesize", pagesize);
        System.out.println("map"+map);
        int i = examMapper.selectsubjects01Count(map);//查询总数
        Map xx=new HashMap();
        xx.put("count",i);
        xx.put("data",examMapper.selectsubjects01(map));
        System.out.println("------------------"+xx);
        return xx;
    }

    @Override
    public List<Map> selectSubject2() {

            String key="exam:subject";
            if(redisTemplate.hasKey(key)){
                return (List<Map>) redisTemplate.opsForValue().get(key);
            }
            //查询所有
            List<Map> maps = examMapper.selectSubject2();
            redisTemplate.opsForValue().set(key,maps);
            return maps;
        }

    @Override
    public List<Map> selectDirections() {
        return examMapper.selectDirections();
    }

    //新增试卷
    //开启事务
    @Transactional
    @Override
    public int insertExamination(Map map) {
        System.out.println(map);
        Integer examinationId = (Integer) map.get("examinationId");
        String examinationName = (String) map.get("examinationName");
        Integer examinationTeacherId = (Integer) map.get("examinationTeacherId");
        Integer examinationType = (Integer) map.get("examinationType");
//        Double examinationScore = (Double) map.get("examinationScore");
        Object examinationScore = map.get("examinationScore");
        Double examinationScore1 = Double.valueOf(examinationScore.toString());
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        String formattedDateTime = zonedDateTime.format(formatter);
        String examinationTime = formattedDateTime;
        System.out.println("当前时间：" + formattedDateTime);
//        Double examinationScore =  Double.valueOf((String) map.get("examinationScore");
        List<Map> subjectIds = (List<Map>) map.get("questions");

        Map aa=new HashMap<>();
        Integer id = 0;
        //判断是新增还是修改
        if (examinationId == 0){
            // 创建 Examination 对象并赋值
            Examination examination = new Examination(examinationTime,examinationName, examinationTeacherId, examinationType, examinationScore1);
            examMapper.insertExamination(examination);
            id = examMapper.selectExaminationId(examination);
        }else {
            aa.put("examinationId",examinationId);
            aa.put("examinationName",examinationName);
            aa.put("examinationTeacherId",examinationTeacherId);
            aa.put("examinationType",examinationType);
            aa.put("examinationScore",examinationScore);

            //修改试卷表里的信息
            examMapper.updataExaminationInfo(aa);
            id=examinationId;
            //先删除之前的数据
            examMapper.deleteQuestiones(id);
        }
        //获取前端传过来的分数和试题id然后插入数据库
        for (Map map1 : subjectIds){
            //将试卷的id存进试题表里的试卷id字段中
            map1.put("examinationId",id);

        }

        int i = examMapper.insertExaminationSubject(subjectIds);

        return i;
    }
    //查询标签
    @Override
    public List<Map> selectAllTag(Integer major_id) {
        return examMapper.selectAllTag(major_id);
    }

    // 查询某试卷的基础信息
    @Override
    public ExaminationDetail selectExamination(Integer examination_id) {
        // 查询基础试卷信息
        Map<String, Object> examinationInfo = examMapper.selectExaminationSubject(examination_id);
        // 查询某试卷的试题的id
        List<Map<String, Object>> questions = new ArrayList<>();
        List<Map> maps = examMapper.selectQuestionesSubject(examination_id);
        for (Map<String, Object> questionId : maps) {
            List<Map> maps1 = examMapper.selectSubjectmessage(questionId);
            if (!maps1.isEmpty()) {
                questions.add(maps1.get(0));
            }
        }
        // 返回查询到的所有值
        return new ExaminationDetail(examinationInfo, questions);
    }


}
